<html id="whole">

<head>
<link rel="shortcut icon" type="image/x-icon" href="d2.ico" />
<title>github issue转CSDN博客工具</title>
</head>

<style>

#raw, #result {
	background-color: rgb(199,237,204);
}

#whole {
	background-color: black;
}
</style>
<textarea id="raw" rows="40" cols="80" onkeydown="keySend(event);">
</textarea>
<textarea id="result" rows="40" cols="80">
把github issue的markdown源代码粘贴过来，会自动转成CSDN markdown博客内容
</textarea>
<button id="jerrybutton" onclick="myFunction()">Click me</button>

<script>
	function keySend(event) {
		if (event.ctrlKey && event.keyCode == 13) {
			var button=document.getElementById("jerrybutton"); 
			button.focus(); 
			button.onclick(); 
		}
	}
</script>
<script>

/*
![clipboard3](https://user-images.githubusercontent.com/5669954/32415087-7b4d8a06-c26e-11e7-9122-c818d5f31586.png)

2017-11-11 1：11PM [图片] and ![imageTag] must be matched.
*/


function getNodeTobeReplaced(current, parent, newPicUrl ){
	return {
		"current": current,
		"parent": parent,
		"newPicUrl": newPicUrl
	};
}

/*

![clipboard3](https://user-images.githubusercontent.com/5669954/32415087-7b4d8a06-c26e-11e7-9122-c818d5f31586.png)
*/

// var IMAGE_PATTERN = /^!\[(.*)\].*$/;

// Jerry 2019-6-13 github image tag has changed?!

/*
<img width="587" alt="clipboard1" src="https://user-images.githubusercontent.com/5669954/59403437-2af8a700-8dd5-11e9-9ab3-bf9f1b7cbbea.png">

*/
var IMAGE_PATTERN = /^!\[(.*)\]\((.*)\).*$/;///^.*src="(.*).*/;
var IMAGE_NAME = /^clipboard(\d+).*$/;
var IMAGE_TAG = "[图片]";
var OLD_IMAGE_INDEX = 1;

function SortedImage(index, url){
	this.index = index;
	this.url = url;
}

function sortByIndex(v1,v2){
		if(v1.index < v2.index ){
			return -1;
		}
		else if(v1.index > v2.index ){
			return 1;
		}
		else return 0;
}

// complete url means: ![clipboard3](https://user-images.githubusercontent.com/5669954/32415087-7b4d8a06-c26e-11e7-9122-c818d5f31586.png)
function getImageIndexAndComplateUrl(trimed){

	var result = IMAGE_PATTERN.exec(trimed);

	if(!result){ // pattern fails to match
		return null;
	}
	// result 0 : whole image markdown
	// result 1 : "clipboard1"
	if( result.length != 3){
		return null;
	}
	var index = -1;
	if( result[1] === "image"){
		index = OLD_IMAGE_INDEX++;
		return new SortedImage(index, result[2]);
	}
	var imageName = IMAGE_NAME.exec(result[1]);
	if( imageName === null)
		return null;
	if( imageName.length != 2) {
		return null;
	}
	index = parseInt(imageName[1]);
	return new SortedImage(index, result[2]);
}

/*
<img width="587" alt="clipboard1" src="https://user-images.githubusercontent.com/5669954/59403437-2af8a700-8dd5-11e9-9ab3-bf9f1b7cbbea.png">

Jerry 2019-09-13 10:10PM mid autumn holiday

for some old issue, see pattern below:

![image](https://cloud.githubusercontent.com/assets/5669954/26522769/4eab953c-4308-11e7-8f36-aed394b1d46e.png)

*/

function isImage(trimed){
	if ( trimed.indexOf("clipboard") > 0 && trimed.indexOf("![") === 0)
		return true;
	if ( trimed.indexOf("image") > 0 && trimed.indexOf("![") === 0)
		return true;
	return false;
}

function isOriginalSignature(origin){
	if( (origin.indexOf("原创文章") > 0) && (origin.indexOf("关注公众号") > 0))
		return true;
	if( origin.indexOf("[公众号截图]") > 0)
		return true;
	if( origin.indexOf("https://upload-images.jianshu.io/upload_images/2085791-c4f58b9813a954cb.png") > 0)
		return true;
	if( origin.indexOf("upload-images.jianshu.io/upload_images/2085791-dcc88e58a67cac8c.png") > 0)
		return true;
	
	return false;
}

function extractImage(source,raw,imagePool){
	var splitted = source.split("\n");
	for( var i = 0; i < splitted.length; i++){
		var origin = splitted[i];
		var trimed = splitted[i].trim();
		if( isImage(trimed) ) { 
			var storedImage = getImageIndexAndComplateUrl(trimed);
			if( storedImage)
				imagePool.push(storedImage);
			else{
				if( isOriginalSignature(origin)){
					continue;
				}
				else
					alert("caution! Corrupt image tag!");
			}
		}
		// Jerry: for CSDN blog, the raw image markdown source should also be reserved
		// 要获取更多Jerry的原创文章，请关注公众号"汪子熙":
		if( isOriginalSignature(origin)){
			continue;
		}
		raw.push(origin);
	}
	imagePool.sort(sortByIndex);
}

function replaceImageTag(raw,imagePool){
	var formatted = "";
	var line = "";
	for( var i = 0; i < raw.length; i++){
		if( !isImage(raw[i])) {
			line = raw[i];
		}
		else{
			line = imagePool.shift().url;
			line = "<img src=\"" + line + "\">";
		}
		formatted = formatted + "\n" + line;
	}
	return formatted;
}

// Jerry： main logic!!!!!!!!!!!!!!

function myFunction(){
	var source = document.getElementById("raw");
	var html = source.value;
	var rawItem = [];
	var imagePool = [];
	extractImage(html,rawItem, imagePool);
	var formatted = replaceImageTag(rawItem,imagePool);
	var target = document.getElementById("result");
	target.value = addSignature(formatted);
	copyToClipboard(target);
	clearInput();
}

function clearInput(){
	var source = document.getElementById("raw");
	source.value = "";
	OLD_IMAGE_INDEX = 1;
}

function copyToClipboard(node) {
  node.select();

  try {
    var successful = document.execCommand('copy');
    var msg = successful ? 'successful' : 'unsuccessful';
    // alert('Copying text command was ' + msg);
  } catch (err) {
    alert('Oops, unable to copy: ' + msg);
  }
}

function addSignature(raw){
	var jerry = "<img src=\"https://user-images.githubusercontent.com/5669954/61616089-2a87e180-ac9a-11e9-861d-c29c2cf897af.png\">";
	var result = raw + "要获取更多Jerry的原创文章，请关注公众号\"汪子熙\":";
	result = result + "\n" + jerry;
	return result;
}
</script>
</html>